package com.sodaopen.crm.sys.controller;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sodaopen.crm.common.entity.AjaxResult;
import com.sodaopen.crm.sys.entity.User;
import com.sodaopen.crm.sys.service.IUserService;

import cn.hutool.core.map.MapUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SodaJava
 * @since 2020-07-02
 */
@RestController
public class UserController {
	
	@Autowired
	private IUserService userService;

	@GetMapping("/users")
	public Object queryUser(
			@RequestParam Map<String, Object> params,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit) {
		
		Page<User> result = userService.queryUsers(params, page, limit);

		return AjaxResult.OK("查询成功！", result);
	}
	
	
	@PostMapping("/users")
	public Object saveUser(@Validated @RequestBody User user) {
		user.setPassword("123456"); //新增用户的默认密码
		user.setAvailable(true);
		boolean flag = userService.save(user);
		if(flag) {
			return AjaxResult.CREATED("添加用户成功！", user);
		}else {
			return AjaxResult.ERROR("添加用户失败！");
		}
	}

	@PutMapping("/users/{uid}/available/{available}")
	public Object updateStatus(@PathVariable(name="uid") Integer uid, @PathVariable("available") Boolean available){
		User user = userService.getById(uid);
		if(user == null){
			return AjaxResult.NOT_FOUND("用户不存在！");
		}

		user.setAvailable(available);
		boolean flag = userService.updateById(user);
		if(flag){
			return AjaxResult.OK("修改用户状态成功！", user);
		}else{
			return AjaxResult.ERROR("修改用户状态失败！");
		}
	}

	@GetMapping("/users/{id}")
	public Object getUser(@PathVariable("id") Integer id){
		User user = userService.getById(id);
		if(user == null){
			return AjaxResult.NOT_FOUND("用户不存在！");
		}
		return AjaxResult.OK("查询成功！", user);
	}

	@PutMapping("/users/{id}")
	public Object updateUser(@PathVariable("id") Integer id,
							 @RequestBody User user){

		User dbUser = userService.getById(id);
		if(dbUser == null){
			return AjaxResult.NOT_FOUND("用户不存在！");
		}

		//复制要被更新的字段
		dbUser.setName(user.getName());
		dbUser.setMobile(user.getMobile());
		dbUser.setEmail(user.getEmail());

		//空的字段不会更新到数据库
		boolean flag = userService.updateById(dbUser);
		if(flag){
			return AjaxResult.OK("更新用户信息成功！", dbUser);
		}else{
			return AjaxResult.ERROR("更新用户信息失败！");
		}

	}

	@DeleteMapping("/users/{id}")
	public Object deleteUser(@PathVariable("id") Integer id){
		boolean flag = userService.removeById(id);
		if(flag){
			return AjaxResult.DELETED("删除成功！");
		}else{
			return AjaxResult.ERROR("删除失败！");
		}
	}

	/**
	 * 检查用户名是否存在
	 * @param username
	 * @return
	 */
	@GetMapping("/users/exists/{username}")
	public Object getUserByUsername(@PathVariable("username") String username){
		User user = userService.findByUsername(username);
		if(user != null) {
			return AjaxResult.OK("用户已存在！", MapUtil.builder("exists", true).build());
		}else{
			return AjaxResult.OK("用户不存在！", MapUtil.builder("exists", false).build());
		}
	}
}
